﻿using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;
using ProductManagement.Infrstructre.Interface;

namespace ProductManagement.Applcation.CommandHanlers
{
    public class MessageSelectCommandHandlers : IRequestHandler<MessageSelectCommand, MessageSelectDtos>
    {
        protected readonly InterfaceMessageRepository interfaceMessage;

        public MessageSelectCommandHandlers(InterfaceMessageRepository interfaceMessage)
        {
            this.interfaceMessage = interfaceMessage;
        }

        public async Task<MessageSelectDtos> Handle(MessageSelectCommand request, CancellationToken cancellationToken)
        {
            var size = (request.pageindex - 1) * request.pagesize;
            var newlist = (await interfaceMessage.GetAll()).ToList();
            MessageSelectDtos page = new MessageSelectDtos();
            page.pagecount = newlist.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / request.pagesize);
            page.list = newlist.Skip(size).Take(request.pagesize).ToList();
            return page;
        }
    }
}
